	package edu.usc.libtalk;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import edu.usc.libtalk.adapter.LibArrayAdapter;
import edu.usc.libtalk.adapter.LibListAdapter;
import edu.usc.libtalk.utils.ApplicationService;

public class LibTabListActivity extends Activity{

	private boolean isLibTabListActivity=true;
	public static Activity libTabListActivity;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* this view can be changed back to the grid view by
         * modifying the content view below to the original
         * value of activity_lib_tab_list */
        setContentView(R.layout.activity_lib_tab_list2);
        
        if(isLibTabListActivity==true)
        {
            MainActivity.mainActivity.finish();        	
        }

        
        ListView listView = (ListView) findViewById(R.id.lib_list_listview);
        String[] defaults = {"library1", "library2", "library3", "library4"};
        listView.setAdapter(new LibArrayAdapter(this, defaults));
        
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            	Intent intent = new Intent(LibTabListActivity.this, SignInActivity.class);            	
            	TextView textView = (TextView)v.findViewById(R.id.label);
            	ApplicationService.setLibrary(textView.getText().toString());
            	startActivity(intent);
            }
        });
    }
    
    @Override
    public void onBackPressed()
    {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(LibTabListActivity.this);
		alertDialog.setTitle("Exit this application");
		alertDialog.setMessage("Are you sure you want to close this application?");
		
		alertDialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				
				// Go to home screen
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);						
			}});
		
		alertDialog.setNegativeButton("Cancel", null);		
		
		alertDialog.show();
    }

}
